package com.jss.lanouhn.fristapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.adapters.AuthorRVContentAdapter;
import com.jss.lanouhn.fristapp.modles.HomeAuthorContentInfo;
import com.jss.lanouhn.fristapp.modles.homeModles.HomeAuthorTitle;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/8/31.
 */
public class HomeAuthorActivity extends AppCompatActivity {

    private String titleUrlHead = "http://api.liwushuo.com/v2/post_authors_v2/";
    private String titleUrlEnd = "?limit=20&offset=0";
    private String contentUrlHead = "http://api.liwushuo.com/v2/post_authors/";
    private String contentUrlEnd = "/involved_posts?limit=20&offset=0";
    private String titleUrl, contentUrl;
    private HomeAuthorTitle ATinfos;
    private HomeAuthorContentInfo conInfos;
    private ImageView iv_background,iv_lanmu_ground;
    private TextView tv_author_name,tv_show_all,tv_author_title,tv_title_times;
    private FrameLayout fl_lan,fl_stragety;
    private CircleImageView iv_author_icon;
    private AuthorRVContentAdapter rvAdapter;
    private RecyclerView authorRv;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_author_detail);
        initData();
        ininForWebData();
        initForRVData();
    }




    private void initData() {
        Intent intent = getIntent();
        String authorId = intent.getStringExtra("id");
        titleUrl = titleUrlHead + authorId + titleUrlEnd;
        contentUrl = contentUrlHead + authorId + contentUrlEnd;
        iv_background= (ImageView) findViewById(R.id.iv_background);
        iv_author_icon= (CircleImageView) findViewById(R.id.iv_author_icon);
        tv_author_name= (TextView) findViewById(R.id.tv_author_name);
        fl_lan= (FrameLayout) findViewById(R.id.Fl_home_author);
        fl_stragety= (FrameLayout) findViewById(R.id.Fl_home_strategy);

    }

    private void ininForWebData() {
        OkHttpClient clint = new OkHttpClient();
        Request request = new Request.Builder()
                .url(titleUrl)
                .build();
        Call call = clint.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Gson gson = new Gson();
                ATinfos=gson.fromJson(result,HomeAuthorTitle.class);
                handler.sendEmptyMessage(1);

            }
        });

    }
    private void initForRVData() {
        OkHttpClient clint=new OkHttpClient();
        Request request=new Request.Builder()
                .url(contentUrl)
                .build();
        Call call=clint.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result=response.body().string();
                Gson gson=new Gson();
                conInfos=gson.fromJson(result,HomeAuthorContentInfo.class);
                if (conInfos.getData().getPosts().size()>0){
                    handler.sendEmptyMessage(2);
                }

            }
        });


    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    HomeAuthorTitle.DataBean bean=ATinfos.getData();
                    Picasso.with(HomeAuthorActivity.this).load(bean.getAuthor().getAvatar_url())
                            .into(iv_background);
                    Picasso.with(HomeAuthorActivity.this).load(bean.getAuthor().getAvatar_url())
                            .into(iv_author_icon);
                    tv_author_name.setText(bean.getAuthor().getNickname());
                    if (ATinfos.getData().getColumns_count()!=0){
                        showAuthorLM(ATinfos);
                    }

                    break;
                case 2:
                    addRVcontent(conInfos);
                    break;
            }
        }

        /**
         * 为作者栏目的初始化数据动态添加
         * @param authorTitle
         */
        private void showAuthorLM(HomeAuthorTitle authorTitle){
            HomeAuthorTitle.DataBean bean=authorTitle.getData();
            View view= LayoutInflater.from(HomeAuthorActivity.this)
                    .inflate(R.layout.include_lanmu,null);
//            LinearLayout.LayoutParams params=
//                    new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//
//            view.setLayoutParams(params);
            iv_lanmu_ground= (ImageView) view.findViewById(R.id.iv_lanmu_ground);
            tv_show_all= (TextView) view.findViewById(R.id.tv_show_all);
            tv_author_title= (TextView) view.findViewById(R.id.tv_author_title);
            tv_title_times= (TextView) view.findViewById(R.id.tv_title_times);

            Picasso.with(HomeAuthorActivity.this).load(bean.getColumns().get(0).getCover_image_url())
                    .into(iv_lanmu_ground);
            tv_author_title.setText(bean.getColumns().get(0).getTitle());
            tv_title_times.setText(bean.getColumns().get(0).getSubtitle());
            if (bean.getColumns_count()>1){
                tv_show_all.setVisibility(View.VISIBLE);
            }
            fl_lan.addView(view);
        }

    };

    /**
     * 加载RecyleView
     * @param conInfos
     */
    private void addRVcontent(HomeAuthorContentInfo conInfos){

        View view=LayoutInflater.from(HomeAuthorActivity.this).inflate(R.layout.include_stragety,null);

        rvAdapter=new AuthorRVContentAdapter(view.getContext(),conInfos);
        authorRv= (RecyclerView) view.findViewById(R.id.rv_home_author);

        authorRv.setAdapter(rvAdapter);
        LinearLayoutManager ll=new LinearLayoutManager(HomeAuthorActivity.this);
        ll.setOrientation(LinearLayoutManager.VERTICAL);
        authorRv.setLayoutManager(ll);

        fl_stragety.addView(view);



    }
}
